package com.example.allplan.Activity;

import android.os.Bundle;
import android.telecom.CallAudioState;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.allplan.Fragment.BaseFragment;
import com.example.allplan.JavaBean.NaoZhongJavaBean;
import com.example.allplan.MyApplication;
import com.example.allplan.R;
import com.example.allplan.StaticFangfa.IntZhuanShiJian;

import java.util.Calendar;
import java.util.Date;

public class TianJiaNaoZhongActivity extends BaseActivity implements View.OnClickListener {

    TextView kaishihour, kaishiminute, chixuhour, chixuminute, jieshuhour, jieshuminute;
    EditText mingcheng_edit;
    LinearLayout kaishi, chixu, jieshu;
    int kaishihouri, kaishiminutei, chixuhouri, chixuminutei, jieshuhouri, jieshuminutei;
    int kaishii, chixui, jieshui;
    MyApplication myapp;
    Toolbar mtoolbar;
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tian_jia_nao_zhong_activity_layout);
        init();
    }
    int twointtooneint(int h,int m){
        return h*60+m;
    } int datetooneint(Date o){
        return twointtooneint(o.getHours(),o.getMinutes());
    }
    void init() {
        myapp=MyApplication.getInstance();
        mtoolbar=findViewById(R.id.tian_jia_nao_zhong_toolbar);
        setSupportActionBar(mtoolbar);
        mingcheng_edit = findViewById(R.id.shu_ru_nao_zhong_ming_edittext);
        kaishihour = findViewById(R.id.kai_shi_hour);
        kaishiminute = findViewById(R.id.kai_shi_minute);
        chixuhour = findViewById(R.id.chi_xu_hour);
        chixuminute = findViewById(R.id.chi_xu_minute);
        jieshuhour = findViewById(R.id.jie_shu_hour);
        jieshuminute = findViewById(R.id.jie_shu_minute);
        kaishi = findViewById(R.id.nao_zhong_kai_shi);
        kaishi.setOnClickListener(this);
        chixu = findViewById(R.id.nao_zhong_chi_xu);
        chixu.setOnClickListener(this);
        jieshu = findViewById(R.id.nao_zhong_jie_shu);
        jieshu.setOnClickListener(this);
        duqushijian();
    }
    Date inttodate(int n){
       Date date=new Date();
       date.setHours(n/60);
       date.setMinutes(n%60);
       return date;
    }
    void duqushijian() {

       duqukaishi();
       duquchixu();
       duqujieshu();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.tian_jia_nao_zhong_menu, menu);
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
            case R.id.tian_jia_nao_zhong_wan_cheng:
                if(!mingcheng_edit.getText().toString().equals("")&&chixui>0){
                    myapp.mNaoZhongCaoZuo.writeone(new NaoZhongJavaBean(myapp.nao_zhong_xiabiao++,kaishii,chixui,jieshui,mingcheng_edit.getText().toString()));
                    myapp.putSH_Int(myapp.NAO_ZHONG_XIA_BIAO_SHARE_NAME,myapp.nao_zhong_xiabiao);
                    myapp.putSH_Int(myapp.NAO_ZHONG_SHU_SHARE_NAME,++myapp.nao_zhong_ge_shu);
                    setResult(82);
                    finish();
                }else if(mingcheng_edit.getText().toString().equals("")){
                    Toast.makeText(myapp, "名称不能为空", Toast.LENGTH_SHORT).show();
            }else if(!mingcheng_edit.getText().toString().equals("")&&chixui<=0){
                    Toast.makeText(myapp, "持续时间不能为0", Toast.LENGTH_SHORT).show();
                }
                break;

        }
        return true;
    }

    void duqukaishi(){
        kaishihouri = Integer.parseInt(kaishihour.getText().toString());
        kaishiminutei = Integer.parseInt(kaishiminute.getText().toString());
        kaishii = kaishihouri * 60 + kaishiminutei;
    }
    void duquchixu(){
        chixuhouri = Integer.parseInt(chixuhour.getText().toString());
        chixuminutei = Integer.parseInt(chixuminute.getText().toString());
        chixui = chixuhouri * 60 + chixuminutei;
    }
    void duqujieshu(){
        jieshuhouri = Integer.parseInt(jieshuhour.getText().toString());
        jieshuminutei = Integer.parseInt(jieshuminute.getText().toString());
        jieshui = jieshuhouri * 60 + jieshuminutei;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nao_zhong_kai_shi:
                Calendar m=Calendar.getInstance();
                m.setTime(inttodate(kaishii));
                TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        if(datetooneint(date)<jieshui){
                            kaishihour.setText(IntZhuanShiJian.format(date.getHours()));
                            kaishiminute.setText(IntZhuanShiJian.format(date.getMinutes()));
                            duqukaishi();
                        }else {
                            Toast.makeText(TianJiaNaoZhongActivity.this, "无效，开始时间要小于结束时间", Toast.LENGTH_SHORT).show();
                        }

                    }
                }).setOutSideCancelable(true)
                        .setType(new boolean[]{false, false, false, true, true,false})
                        .isCyclic(true)
                        .setDate(m)
                        .setTitleText("开始时间")
                        .setSubmitColor(getResources().getColor(R.color.lan_se))
                        .setCancelColor(getResources().getColor(R.color.lan_se))
                        .setTitleColor(getResources().getColor(R.color.lan_se))
                        .setBgColor(getResources().getColor(R.color.white))
                        .setTitleBgColor(getResources().getColor(R.color.white))
                        .setTextColorCenter(getResources().getColor(R.color.lan_se))
                        .setTextColorOut(getResources().getColor(R.color.hui_se))
                        .build();
                pvTime.show();
                break;
            case R.id.nao_zhong_chi_xu:
                Calendar m1=Calendar.getInstance();
                m1.setTime(inttodate(chixui));
                TimePickerView pvTime1 = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {

                        chixuhour.setText(IntZhuanShiJian.format(date.getHours()));
                        chixuminute.setText(IntZhuanShiJian.format(date.getMinutes()));
                        duquchixu();


                    }
                }).setOutSideCancelable(true)
                        .setType(new boolean[]{false, false, false, true, true,false})
                        .isCyclic(true)
                        .setDate(m1)
                        .setTitleText("持续时间")
                        .setSubmitColor(getResources().getColor(R.color.lan_se))
                        .setCancelColor(getResources().getColor(R.color.lan_se))
                        .setTitleColor(getResources().getColor(R.color.lan_se))
                        .setBgColor(getResources().getColor(R.color.white))
                        .setTitleBgColor(getResources().getColor(R.color.white))
                        .setTextColorCenter(getResources().getColor(R.color.lan_se))
                        .setTextColorOut(getResources().getColor(R.color.hui_se))
                        .build();
                pvTime1.show();
                break;
            case R.id.nao_zhong_jie_shu:
                Calendar m2=Calendar.getInstance();
                m2.setTime(inttodate(jieshui));
                TimePickerView pvTime2 = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {



                        if(datetooneint(date)>kaishii){
                            jieshuhour.setText(IntZhuanShiJian.format(date.getHours()));
                            jieshuminute.setText(IntZhuanShiJian.format(date.getMinutes()));
                            duqujieshu();
                        }else {
                            Toast.makeText(TianJiaNaoZhongActivity.this, "无效，结束时间要大于开始时间", Toast.LENGTH_SHORT).show();
                        }
                    }
                }).setOutSideCancelable(true)
                        .setType(new boolean[]{false, false, false, true, true,false})
                        .isCyclic(true)
                        .setDate(m2)
                        .setTitleText("结束时间")
                        .setSubmitColor(getResources().getColor(R.color.lan_se))
                        .setCancelColor(getResources().getColor(R.color.lan_se))
                        .setTitleColor(getResources().getColor(R.color.lan_se))
                        .setBgColor(getResources().getColor(R.color.white))
                        .setTitleBgColor(getResources().getColor(R.color.white))
                        .setTextColorCenter(getResources().getColor(R.color.lan_se))
                        .setTextColorOut(getResources().getColor(R.color.hui_se))
                        .build();
                pvTime2.show();
                break;
        }
    }
}
